package com.cndw.kungfu.event.once;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.cndw.kungfu.comm.JXSpreadTask;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.Friend;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataStaticCenter;
import com.cndw.kungfu.model.disciple.PlayerMartial;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.disciple.domain.DiscipleCompararor;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.EventNotice;
import com.cndw.kungfu.vo.PlayerChangeVO;
import com.cndw.rpg.framework.ResponseMessage;

/**
 * 角色升级的处理事务
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-12-5 下午11:38:01
 */
public class PlayerUpgradeTask implements Runnable {

	private Player player;
	private int level;

	public PlayerUpgradeTask(Player player, int level) {
		this.player = player;
		this.level = level;
	}

	@Override
	public void run() {

		try {
			boss();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void boss() {
		try {
			//秘典任务
			Context.getTaskServer().checkAwaitSecret(player.getPlayerId());
			Context.getTaskServer().autoRaidioTask(player.getPlayerId(), 1);
			// 自動升級徒弟
			proDisciple();

			PlayerChangeVO change = new PlayerChangeVO();
			change.put(PlayerChangeVO.PLAYER_MAIN, player.toPlayerVO());
			change.put(PlayerChangeVO.campMax, player.getCampNum());
			change.put(PlayerChangeVO.discipleMax, player.getDescipleMax());
			Context.getTcpServer().broadcast(change, ConstantsRadio.playerChange, player.getPlayerId());

			Context.getGeneralServer().addTilte(player.getPlayerId(), Constants.titleLevelUp, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			friend();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			if (level >= 10 ) {
				JXSpreadTask.complete(player, JXSpreadTask.level10);
			}else if (level >= 20) {
				JXSpreadTask.complete(player, JXSpreadTask.level20);
			}else if (level >= 30) {
				JXSpreadTask.complete(player, JXSpreadTask.level30);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void proDisciple() {
		// 自動升級徒弟
		if (player.getDataInfo().intDataGet(ConstantsIntKey.autoTrain) == 1) {
			PlayerMartial martial = Context.getDiscipleServer().getByPlayerId(player.getPlayerId());

			Collection<Disciple> disciples = martial.getDisciples();
			if (disciples != null) {
				ArrayList<Disciple> list = new ArrayList<Disciple>(disciples);
				Collections.sort(list, new DiscipleCompararor());
				boolean isUp = false;
				for (Disciple disciple : list) {
					if (player.getExpPool() <= 7 || disciple.getCampId() == 0) {
						break;
					}
					int costAll = 0;
					int oldLevel = disciple.getLevel();
					while (disciple.getLevel() < player.getLevel() && disciple.getExpNeed() <= player.getExpPool()) {
						long alter = disciple.getExpNeed();
						costAll += alter;
						disciple.alterExp(alter);
						disciple.clacScore();
						disciple.commitSync();
						player.alterExpPool(-alter);
						isUp = true;
					}
					if (costAll > 0) {
						LogServer.recordLog(player, ConstantsLog.disciple, 80023, disciple.getName(), oldLevel, disciple.getLevel(), costAll);

						ResponseMessage message = ResponseMessage.chatMsg(LanguageLib.get("disciple.auto.upgrade", disciple.getName(),
								LangUtil.getColor(disciple.getQuality()), disciple.getLevel()));
						Context.getTcpServer().broadcastResMsg(message, player.getPlayerId());
					}
				}

				if (isUp) {
					Context.getPlayerServer().clacProperties(player);
				}
			}

		}
	}

	private void friend() {
		if (level == 12 || level == 20 || level == 25) {
			DataStaticCenter.leftFindAlter(player.getPlayerId(), 4);
		} else if (level == 30 || level == 35) {
			DataStaticCenter.leftFindAlter(player.getPlayerId(), 2);
		}

		// 处理好友事件
		if (player.getLevel() >= ConstantsModel.friendCongLevel[0] && player.getLevel() < ConstantsModel.friendCongLevel[1]) {
			Collection<Friend> friends = player.getDataInfo().getFriends();
			if (friends != null) {
				Set<Long> revIds = new HashSet<Long>();
				for (Friend friend : friends) {

					if (Context.getPlayerServer().isOnline(friend.getFriendId())) {// 在线好友收到祝福广播
						Player target = Context.getPlayerServer().getByPlayerId(friend.getFriendId());
						if (target.getLevel() >= ConstantsModel.friendCongLevel[0] && target.getLevel() < ConstantsModel.friendCongLevel[1]
								&& target.getDataInfo().isFriend(player.getPlayerId())) {

							DataStaticCenter.friendAwardAdd(player.getPlayerId(), friend.getFriendId(), level);

							revIds.add(friend.getFriendId());
						}

					}

				}
				if (!revIds.isEmpty()) {
					EventNotice eventNotice = new EventNotice(ConstantsRadio.friend, ConstantsRadio.friendUpgrade);
					eventNotice.put("uid", player.getPlayerId());
					eventNotice.put("uname", player.getPlayerName());
					eventNotice.put("uV", player.getVocationId());
					eventNotice.put("upLevel", level);
					eventNotice.put("o", 0);// 升级最开始
					eventNotice.broadcast(revIds);
				}

			}

		}
	}

}
